package test;

/**
 * Created by Caesar on 2016/4/8.
 */
public class SnakePalindrome {

    public int[][] a(int start, int n) {
        int[][] res = new int[n][n];
        int[][] res2=null;

        if (n == 1) {
            res[0][0] = start + 1;
            return res;
        }
        //填充第1行
        for (int i = 0; i < n; i++)
            res[0][i] = start + i + 1;
        //填充第n列
        for (int i = 1; i < n; i++)
            res[i][n - 1] = start + i + n;
        //填充第n行
        for (int i = 0; i < n - 1; i++)
            res[n - 1][i] = start + 3 * n - 2 - i;
        //填充第1列
        for (int i = 1; i < n - 1; i++)
            res[i][0] = start + 4 * n - 3 - i;

        if (n - 2 == 0)
            return res;
        else
            res2 = a(res[1][0], n - 2);

        for (int i = 1; i < n - 1; i++)
            for (int j = 1; j < n - 1; j++)
                res[i][j] = res2[i - 1][j - 1];
        return res;
    }

    public static void main(String[] args) {
        SnakePalindrome sp = new SnakePalindrome();
        int n=30;
        int[][] res= sp.a(0, n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                System.out.format("%3d ", res[i][j]);
            System.out.println();
        }
    }
}
